
package cn.edu.scau.cmi.hombio.dao;

import cn.edu.scau.cmi.hombio.domain.Pigsty;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Pigsty entities.
 * 
 */
@Repository("PigstyDAO")

@Transactional
public class PigstyDAOImpl extends AbstractJpaDao<Pigsty> implements PigstyDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] {
			Pigsty.class }));

	/**
	 * EntityManager injected by Spring for persistence unit pig
	 *
	 */
	@PersistenceContext(unitName = "pig")
	private EntityManager entityManager;

	/**
	 * Instantiates a new PigstyDAOImpl
	 *
	 */
	public PigstyDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findPigstyByNumber
	 *
	 */
	@Transactional
	public Set<Pigsty> findPigstyByNumber(Integer number) throws DataAccessException {

		return findPigstyByNumber(number, -1, -1);
	}

	/**
	 * JPQL Query - findPigstyByNumber
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pigsty> findPigstyByNumber(Integer number, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigstyByNumber", startResult, maxRows, number);
		return new LinkedHashSet<Pigsty>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigstyById
	 *
	 */
	@Transactional
	public Pigsty findPigstyById(Integer id) throws DataAccessException {

		return findPigstyById(id, -1, -1);
	}

	/**
	 * JPQL Query - findPigstyById
	 *
	 */

	@Transactional
	public Pigsty findPigstyById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPigstyById", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Pigsty) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findPigstyByCapacity
	 *
	 */
	@Transactional
	public Set<Pigsty> findPigstyByCapacity(Integer capacity) throws DataAccessException {

		return findPigstyByCapacity(capacity, -1, -1);
	}

	/**
	 * JPQL Query - findPigstyByCapacity
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pigsty> findPigstyByCapacity(Integer capacity, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigstyByCapacity", startResult, maxRows, capacity);
		return new LinkedHashSet<Pigsty>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigstyByMemoContaining
	 *
	 */
	@Transactional
	public Set<Pigsty> findPigstyByMemoContaining(String memo) throws DataAccessException {

		return findPigstyByMemoContaining(memo, -1, -1);
	}

	/**
	 * JPQL Query - findPigstyByMemoContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pigsty> findPigstyByMemoContaining(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigstyByMemoContaining", startResult, maxRows, memo);
		return new LinkedHashSet<Pigsty>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigstyByMemo
	 *
	 */
	@Transactional
	public Set<Pigsty> findPigstyByMemo(String memo) throws DataAccessException {

		return findPigstyByMemo(memo, -1, -1);
	}

	/**
	 * JPQL Query - findPigstyByMemo
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pigsty> findPigstyByMemo(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigstyByMemo", startResult, maxRows, memo);
		return new LinkedHashSet<Pigsty>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllPigstys
	 *
	 */
	@Transactional
	public Set<Pigsty> findAllPigstys() throws DataAccessException {

		return findAllPigstys(-1, -1);
	}

	/**
	 * JPQL Query - findAllPigstys
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pigsty> findAllPigstys(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllPigstys", startResult, maxRows);
		return new LinkedHashSet<Pigsty>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigstyByPrimaryKey
	 *
	 */
	@Transactional
	public Pigsty findPigstyByPrimaryKey(Integer id) throws DataAccessException {

		return findPigstyByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findPigstyByPrimaryKey
	 *
	 */

	@Transactional
	public Pigsty findPigstyByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPigstyByPrimaryKey", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Pigsty) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findPigstyByRfid
	 *
	 */
	@Transactional
	public Set<Pigsty> findPigstyByRfid(String rfid) throws DataAccessException {

		return findPigstyByRfid(rfid, -1, -1);
	}

	/**
	 * JPQL Query - findPigstyByRfid
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pigsty> findPigstyByRfid(String rfid, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigstyByRfid", startResult, maxRows, rfid);
		return new LinkedHashSet<Pigsty>(query.getResultList());
	}

	/**
	 * JPQL Query - findPigstyByRfidContaining
	 *
	 */
	@Transactional
	public Set<Pigsty> findPigstyByRfidContaining(String rfid) throws DataAccessException {

		return findPigstyByRfidContaining(rfid, -1, -1);
	}

	/**
	 * JPQL Query - findPigstyByRfidContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Pigsty> findPigstyByRfidContaining(String rfid, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPigstyByRfidContaining", startResult, maxRows, rfid);
		return new LinkedHashSet<Pigsty>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Pigsty entity) {
		return true;
	}
}
